package solutions.jianzhi;

import solutions.lib.BaseSolution;

/**
 * @author lizhidong
 * <a href="https://leetcode-cn.com/problems/N6YdxV/">剑指 Offer II 068. 查找插入位置</a>
 */
public class Solution68 extends BaseSolution {
    public int searchInsert(int[] nums, int target) {
        int l = 0, r = nums.length - 1;
        while (l <= r) {
            int mid = (l + r) / 2;
            if (target == nums[mid]) {
                return mid;
            } else if (target > nums[mid]) {
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        return l;
    }

    public static void main(String[] args) {
        Solution68 solution68 = new Solution68();
        System.out.println(solution68.searchInsert(new int[] {1,3}, 4));
    }
}
